<?php
// +-------------------------------------------------------------------------+
// | Mobile Melody Plug-in for PHP Melody ( www.phpsugar.com )
// +-------------------------------------------------------------------------+
// | MOBILE MELODY IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +-------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2012 PhpSugar.com. All rights reserved.
// +-------------------------------------------------------------------------+

$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = trim(strtolower($useragent));
$new_URL = str_replace("www.", '', _URL);
$new_URL_MOBI = str_replace("www.", '', _URL_MOBI);
$current_URL = str_replace("www.", '', "http://".$_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
$current_URL_device = strpos($current_URL, $new_URL_MOBI);
$device_url = ($current_URL_device === false) ? 'desktop' : 'mobile';

if ( ! defined('COOKIE_DEVICE'))
{
	define('COOKIE_DEVICE', 'melody_device');
}

if(isset($_COOKIE[COOKIE_DEVICE]))
{
	if ($_COOKIE[COOKIE_DEVICE] != $device_url)
	{
		if ($device_url == 'mobile')
		{
			$takemehome = str_replace($new_URL_MOBI, $new_URL, $current_URL);
			header("Location: ".$takemehome."");
			exit;
		}
		elseif ($device_url == 'desktop')
		{
			$takemehome = str_replace($new_URL, $new_URL_MOBI, $current_URL);
			header("Location: ".$takemehome."");
			exit;
		}
	}
}
else
{
	if (strpos($useragent, "iphone") !== false || strpos($useragent, "symbianos") !== false || strpos($useragent, "ipad") !== false || strpos($useragent, "ipod") !== false || strpos($useragent, "android") !== false || strpos($useragent, "blackberry") !== false || strpos($useragent, "samsung") !== false || strpos($useragent, "nokia") !== false || strpos($useragent, "windows ce") !== false || strpos($useragent, "sonyericsson") !== false || strpos($useragent, "webos") !== false || strpos($useragent, "wap") !== false || strpos($useragent, "motor") !== false || strpos($useragent, "symbian") !== false || strpos($useragent, "android") !== false)
       {
          $device = 'mobile';
          setcookie(COOKIE_DEVICE, $device, time() + 84000, COOKIE_PATH);
          
          if (strpos($current_URL, $new_URL_MOBI) === false)
          {
             //User is NOT on mobile site, redirect
             $takemehome = str_replace($new_URL, $new_URL_MOBI, $current_URL);
             header("Location: ".$takemehome."");
             exit;
          }
          //Continue as usual
       }
       else
       {
          $device = 'desktop';
          setcookie(COOKIE_DEVICE, $device, time() + 84000, COOKIE_PATH);
          
          if (strpos($current_URL, $new_URL_MOBI) !== false)
          {
             //User IS on mobile site, redirect
             $takemehome = str_replace($new_URL_MOBI, $new_URL, $current_URL);
             header("Location: ".$takemehome."");
             exit;
          }
          //Continue as usual
       }
}
?>
